package com.programmercarl.tree;

/**
 * https://leetcode.cn/problems/trim-a-binary-search-tree/description/
 *
 * @author yulongtian
 * @create 2024-12-26 9:36
 */
public class Test25 {
    public static void main(String[] args) {

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode trimBST(TreeNode root, int low, int high) {
        if (root == null) {
            return null;
        }
        if (low > root.val) {
            return trimBST(root.right, low, high);
        }
        if (high < root.val) {
            return trimBST(root.left, low, high);
        }
        root.left = trimBST(root.left, low, high);
        root.right = trimBST(root.right, low, high);

        return root;
    }
}
